Quick Start
Install:
npm install dotenv-gad
# or
bun add dotenv-gad
Define a schema (env.schema.ts):
import { defineSchema } from 'dotenv-gad';
export default defineSchema({
PORT: { type: 'number', default: 3000, docs: 'Port to run the server on' },
DATABASE_URL: { type: 'string', required: true, sensitive: true },
});
Load and validate your environment:
import { loadEnv } from 'dotenv-gad';
import schema from './env.schema';
const env = loadEnv(schema);
console.log(env.PORT); // number — fully typed
loadEnv reads from both process.env and your .env file (if present). Following dotenv conventions, variables already set in the real environment take priority over .env file values — the file provides development defaults, and platform-injected variables (Vercel, Railway, Docker, AWS Lambda) work out of the box with no .env file required. Pass override: true if you want file values to win instead.
Options
strict— when true, fail on environment variables not present in the schema.includeRaw— include raw values in error reports (non-sensitive by default).includeSensitive— when used withincludeRawwill reveal values marked sensitive (use only for local debugging).path— path to a custom.envfile (defaults to.envin cwd).override— when true,.envfile values replace variables already set in the real environment (default: the real environment wins).
Using EnvValidator directly
For more control (e.g., validating a custom env record without loading from a file):
import { EnvValidator } from 'dotenv-gad';
import schema from './env.schema';
const validator = new EnvValidator(schema);
const env = validator.validate(process.env);
CLI
npx dotenv-gad check(orbunx dotenv-gad check) — check .env against the schemanpx dotenv-gad sync(orbunx dotenv-gad sync) — generate/update.env.examplenpx dotenv-gad types(orbunx dotenv-gad types) — generateenv.d.ts